﻿#include "log.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
#include <QDateTime>
#include <QDebug>

void setLogPath(const QString & path)
{
    s_logPath = path;

    qDebug() << path;
}

void setLogLevel(int level)
{
    s_logLevel = level;
}

bool static ensureDirExist(const QString &dirPath)
{
    QDir dir(dirPath);
    if (dir.exists())
    {
        return true;
    }

    qDebug() << "dir Path ["
                       << dirPath << "]  do not exist";

    return dir.mkpath(dirPath);
}

void customLogMessageHandler(QtMsgType type, const QMessageLogContext& ctx, const QString& msg)
{
    if (type < s_logLevel)
    {
        return;
    }

    QString logInfo;
    QString logTime = QDateTime::currentDateTime().toString("hh-mm-ss-MM-dd-yyyy");
    switch (type)
    {
    case QtDebugMsg:
        logInfo = QString("%1 [Debug] %2").arg(logTime, msg);
        break;

    case QtWarningMsg:
        logInfo = QString("%1 [Warning] %2").arg(logTime, msg);
        break;

    case QtCriticalMsg:
        logInfo = QString("%1 [Critical] %2").arg(logTime, msg);
        break;

    case QtFatalMsg:
        logInfo = QString("%1 [Fatal] %2").arg(logTime, msg);
        abort();
//    case QtInfoMsg:
//        logInfo = QString("%1 [Info] %2").arg(logTime, msg);
//        break;
    }

    s_logMutex.lock();
    QFile outFile(s_logPath);
    QFileInfo fileInfo(outFile);
    if (!ensureDirExist(fileInfo.absoluteDir().absolutePath()))
        return;

    if (!outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
        return;

    QTextStream ts(&outFile);
    ts << logInfo << endl;
    outFile.close();
    s_logMutex.unlock();
}

void remove_old_log_file(){

    //1.获取当前日期
        QDateTime current_date_time = QDateTime::currentDateTime();
        QString date = current_date_time.toString("yyyy-MM-dd");
        //2.获取文件夹路径，遍历文件夹
        QString logPath = QCoreApplication::applicationDirPath()+"/Log";
        QStringList foldernames;
        QStringList filters;
        filters << "*.log";
        QDir dir(logPath);
        //搜索当前目录符合条件的文件
        foreach(QString file, dir.entryList(filters, QDir::Files))
            foldernames += logPath + '/' + file;
        //QFileInfoList fileList = dir.entryInfoList();//当前文件夹下只有日志文件，也可以直接获取全部文件
        //3.比较日期
        if (foldernames.size() > 5)
        {
            foreach(QString fileName, foldernames)
            {
                //获取文件的创建时间
                QFileInfo info(fileName);
                QDateTime dt = info.created();//获取文件创建时间
                int days = dt.daysTo(current_date_time);//获取文件相对于当前时间的天数
                if (days > 5)
                {
                    QFile::remove(fileName);//大于5天，移除文件
                }
            }
        }

}

void remove_old_png_file(){

    //1.获取当前日期
        QDateTime current_date_time = QDateTime::currentDateTime();
        QString date = current_date_time.toString("yyyy-MM-dd");
        //2.获取文件夹路径，遍历文件夹
        QString logPath = QCoreApplication::applicationDirPath()+"/Log";
        QStringList foldernames;
        QStringList filters;
        filters << "*.png";
        QDir dir(logPath);
        //搜索当前目录符合条件的文件
        foreach(QString file, dir.entryList(filters, QDir::Files))
            foldernames += logPath + '/' + file;
        //QFileInfoList fileList = dir.entryInfoList();//当前文件夹下只有日志文件，也可以直接获取全部文件
        //3.比较日期
        if (foldernames.size() > 5)
        {
            foreach(QString fileName, foldernames)
            {
                //获取文件的创建时间
                QFileInfo info(fileName);
                QDateTime dt = info.created();//获取文件创建时间
                int days = dt.daysTo(current_date_time);//获取文件相对于当前时间的天数
                if (days > 5)
                {
                    QFile::remove(fileName);//大于5天，移除文件
                }
            }
        }

}
